package com.thingple.app.io;

import android.content.Intent;
import android.os.Handler;
import android.util.Log;

import com.thingple.shared_context.SharedContext;
import com.thingple.version.VersionManager;
import com.thingple.version.components.AppComponent;
import com.thingple.version.components.AppInfo;
import com.thingple.version.components.AppInfoListener;
import com.thingple.version.uuid.UUID;

import java.util.List;
import java.util.Map;

/**
 *
 * Created by lism on 2017/12/8.
 */

public class VersionControllHandler {

    private App app;

    private Handler handler = new Handler();

    private boolean flag = true;

    public VersionControllHandler(App app) {
        this.app = app;
    }

    public void update() {
        if (flag) {
            flag = false;
        } else {
            return;
        }
        Log.d("version", "-------------<><>><><><> update apps");
        new Thread(new Runnable() {
            @Override
            public void run() {
                final VersionManager versionManager = VersionManager.shareInstance();
                versionManager.getUpdateInfo(new AppInfoListener() {
                    @Override
                    public void onListener(UUID uuid, AppInfo appInfo) {

                        if (uuid != null && uuid.uuid != null) {
                            if (appInfo != null) {
                                updateSettings(appInfo);
                            }
                        } else {
                            // wu
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Intent intent = new Intent();
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    intent.setClass(app, LicenceActivity.class);
                                    app.startActivity(intent);
                                }
                            });
                        }
                    }
                });
            }
        }).start();
    }

    private void updateSettings(AppInfo appInfo) {

        boolean settingsChanged = false;
        final VersionManager versionManager = VersionManager.shareInstance();
        final SharedContext sharedContext = SharedContext.shareInstance();
        Map<String, String> settings = appInfo.settings;
        if (settings != null) {
            for (String key : settings.keySet()) {
                String val = settings.get(key);
                String expect = sharedContext.get(key);
                if (expect == null || !val.equals(expect)) {
                    settingsChanged = true;
                    sharedContext.set(key, val);
                }
            }
            if (settingsChanged) {
                // restart app
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        app.restart();
                    }
                });
            }
        }
        List<AppComponent> components = appInfo.components;
        if (components != null && !settingsChanged) {
            Log.d("", "------>>>>>>>安装apk:" + components.size());
            versionManager.updateComponents(components);
        }
    }
}
